<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\MemberFollowAndFans;
use App\Admin\Actions\Grid\OperateMemberVip;
use App\Admin\Repositories\Member;
use App\Constants\MiniProgramConstant;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class MemberController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Member(), function (Grid $grid) {
            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('mini_type', array_keys($map));

            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('created_at')->display(function ($created){
                $date = date('Y-m-d', strtotime($created));
                $his = date('H:i:s', strtotime($created));

                return "<span>{$date}</span><br><span>{$his}</span><br>";
            });
            $grid->column('mini_type')->display(function ($miniType) use($map) {
                return $miniType ? $map[$miniType] : '';
            });
            $grid->column('avatar')->image(' ', 50, 50);
            $grid->column('member_info', '信息')->view('admin.member.member_info');
            $grid->column('vip', 'vip信息')->view('admin.member.vip_info');
            $grid->column('sign');
            $grid->column('open_id');
            $grid->column('integral');
            $grid->column('comment_count');
            $grid->column('favs_count');
            $grid->column('relay_count');
            $grid->column('status')->switch('', true);

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append('<a style="margin-right:5px" href="' . admin_route('member_integral_log.index', ['member_id' => $actions->row->id]) . '">积分日志</a>');
                $actions->append('<a style="margin-right:5px" href="' . admin_route('member_sign_log.index', ['member_id' => $actions->row->id]) . '">签到日志</a>');
                $actions->append('<a style="margin-right:5px" href="' . admin_route('member_task_log.index', ['member_id' => $actions->row->id]) . '">每日任务日志</a><br>');
                $actions->append(new OperateMemberVip(\App\Models\Member::class));
                $actions->append(new MemberFollowAndFans());
            });

            $grid->disableCreateButton();
            $grid->disableFilterButton();
            $grid->disableRefreshButton();
            $grid->disableViewButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableRowSelector();

            $grid->filter(function (Grid\Filter $filter) use($map) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->equal('invite_id')->width(2);
                $filter->equal('mini_type')->select($map)->width(2);
                $filter->equal('nickname')->width(2);
                $filter->equal('phone')->width(2);
                $filter->between('created_at')->datetime()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Member(), function (Show $show) {
            $show->field('id');
            $show->field('nickname');
            $show->field('sex');
            $show->field('avatar');
            $show->field('sign');
            $show->field('open_id');
            $show->field('comment_count');
            $show->field('favs_count');
            $show->field('relay_count');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Member(), function (Form $form) {
            $form->display('id');
            $form->text('nickname');
            $form->text('sex');
            $form->text('avatar');
            $form->text('sign');
            $form->text('open_id');
            $form->text('comment_count');
            $form->text('favs_count');
            $form->text('relay_count');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
